package com.restaurant.serviceimpl;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.restaurant.domain.Inventory;
import com.restaurant.service.InventoryService;

public class InventoryServiceImpl implements InventoryService {

   private HibernateTemplate hibernateTemplate;

   @Autowired
   public void setSessionFactory(SessionFactory sessionFactory){
       this.hibernateTemplate = new HibernateTemplate(sessionFactory);
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Inventory addOrModify( Inventory objInventory ) {
       return ( Inventory ) hibernateTemplate.merge( objInventory );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Inventory deleteRecord( Inventory objInventory ) {
       hibernateTemplate.delete( objInventory );
       return objInventory;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Inventory modifyRecord( Inventory objInventory ) {
       return ( Inventory ) hibernateTemplate.merge( objInventory );
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Inventory> loadAll() {
       return (List<Inventory>)hibernateTemplate.findByNamedQuery( "Inventory.findAll");
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Inventory loadByPrimaryKey( Long primaryKey ) {
       return (Inventory) hibernateTemplate.get( Inventory.class, primaryKey );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Inventory deleteByPrimaryKey( Long primaryKey ) {
       return deleteRecord(loadByPrimaryKey(primaryKey));
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Inventory> loadByHql(String query) {
       return ( List<Inventory> ) hibernateTemplate.find( query );
   }
}